package com.tsingteng.cosfun.mvp.presenter;




import com.tsingteng.cosfun.mvp.view.IView;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * 描述: presenter 基类
 * 创建人: 肖利
 *时间: 2018/7/30
 * qq: (872179966)
 */
public class BasePresenter<V extends IView> implements IPresenter<V> {
    private V mView;
    //用来存放Disposable的容器
    private CompositeDisposable compositeDisposable;

    @Override
    public void attachView(V view) {
        this.mView = view;
    }

    @Override
    public void detachView() {
        this.mView=null;
    }

    @Override
    public void checkAttachView() {
        if (mView==null) {
            throw new IllegalStateException("view is null");
        }
    }

    @Override
    public V getView() {
        checkAttachView();
        return mView;
    }

    @Override
    public void addDisposable(Disposable disposable) {
        if (compositeDisposable==null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.add(disposable);
    }

    @Override
    public void removeDisposable(Disposable disposable) {
        if (compositeDisposable==null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.remove(disposable);
    }

    @Override
    public void removeAllDisposable() {
        if (compositeDisposable==null) {
            compositeDisposable = new CompositeDisposable();
        }
        compositeDisposable.clear();
    }
}
